﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_CS
{
    public partial class UCBaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach : MyUserBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        private static string PhuLucSo_str;
        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;
            PhuLucSo_str = "Phụ lục " + Request.Params["pl"].ToString();
            CheckPermission(right_check(Request.Params["pl"].ToString()));
            if (!IsPostBack)
            {
                BindDataToDropDownList(ddlKyBaoCao, "KyBaoCao");

                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);

            if (PhuLucSo_str == "Phụ lục 1")
            {
                lblFormName.InnerText = "Phụ lục 1";
                chb_baocaotonghop.Visible = false;
                rl1.Visible = true;
            }
            else
            {

                chb_baocaotonghop.Visible = true;
                rl1.Visible = false;
            }
        }
        private void CheckPermission(string right)
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission(right);
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        private string right_check(string input_)
        {
            string st_re = "";

            if (input_ == "0")
            {
                st_re = "BaoCaoChinh";

            }
            else
                if (input_ == "1")
                {
                    st_re = "PhuLuc1";

                }
                else
                    if (input_ == "2")
                    {
                        st_re = "PhuLuc2";

                    }
                    else
                        if (input_ == "3")
                        {
                            st_re = "PhuLuc3";

                        }
                        else
                            if (input_ == "4")
                            {
                                st_re = "PhuLuc4";

                            }
                            else
                                if (input_ == "5")
                                {
                                    st_re = "PhuLuc5";

                                }
                                else
                                    if (input_ == "6")
                                    {
                                        st_re = "PhuLuc6";

                                    }
                                    else
                                        if (input_ == "7")
                                        {
                                            st_re = "PhuLuc7";

                                        }
                                        else
                                            if (input_ == "8")
                                            {
                                                st_re = "PhuLuc8";

                                            }
            return st_re;

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");

        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            bool hasreport = true;
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng lựa chọn đơn vị muốn xem báo cáo";
            }
            else
            {
                List<SqlParameter> lstParams = new List<SqlParameter>();

                //lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                //lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                if (PhuLucSo_str == "Phụ lục 0")
                {
                    string sql = "Select id, KyBaoCaoID_DM,BaoCaoSo From tblBaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach Where KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                    SqlDataReader myReader = SqlHelper.ExecuteReader(CommandType.Text, sql);
                    myReader.Read();
                    if (myReader.HasRows)
                    {
                        Session["ReportNumber"] = myReader["BaoCaoSo"].ToString();
                        Session["ReportPeriod"] = myReader["KyBaoCaoID_DM"].ToString();
                        Session["SwitchCase"] = "Detail";
                        Session["ReportID"] = myReader["ID"].ToString(); ;
                        Session["ReportFullName"] = "Báo cáo chính";
                        Response.Redirect("PhuLuc_Edit.aspx");
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Không có báo cáo trong kỳ này";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
                        hasreport = false;
                    }
                }
                else
                    if (PhuLucSo_str == "Phụ lục 1")
                    {
                        if (rl1.SelectedIndex == -1)
                        {
                            MyReportSQL = "SELECT * FROM dbo.tblPhuLuc1 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                            MyReportParams = lstParams;
                            MyReportSQLParam = ""; ;
                            MyReportName = "rptPhuLuc1.rpt";
                            MyTableName = "tblPhuLuc1";
                        }
                        else
                        {
                            if (rl1.SelectedValue == "choose1")
                            {
                                MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc1 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                MyReportParams = lstParams;
                                MyReportSQLParam = ""; ;
                                MyReportName = "rptPhuLuc1TongHop.rpt";
                                MyTableName = "tblPhuLuc1TongHop";
                            }
                            else
                            {
                                MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc1 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                MyReportParams = lstParams;
                                MyReportSQLParam = ""; ;
                                MyReportName = "rptPhuLuc1aTongHop.rpt";
                                MyTableName = "tblPhuLuc1TongHop";
                            }
                        }
                    }
                    else
                        if (PhuLucSo_str == "Phụ lục 2")
                        {
                            if (chb_baocaotonghop.Checked == false)
                            {
                                MyReportSQL = "SELECT * FROM dbo.tblPhuLuc2 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                MyReportParams = lstParams;
                                MyReportSQLParam = ""; ;
                                MyReportName = "rptPhuLuc2.rpt";
                                MyTableName = "tblPhuLuc2";
                            }
                            else
                            {
                                MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc2 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                MyReportParams = lstParams;
                                MyReportSQLParam = ""; ;
                                MyReportName = "rptPhuLuc2TongHop.rpt";
                                MyTableName = "tblPhuLuc2TongHop";
                            }

                        }
                        else
                            if (PhuLucSo_str == "Phụ lục 3")
                            {
                                if (chb_baocaotonghop.Checked == false)
                                {
                                    MyReportSQL = "SELECT * FROM dbo.tblPhuLuc3 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                    MyReportParams = lstParams;
                                    MyReportSQLParam = ""; ;
                                    MyReportName = "rptPhuLuc3.rpt";
                                    MyTableName = "tblPhuLuc3";
                                }
                                else
                                {
                                    MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc3 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                    MyReportParams = lstParams;
                                    MyReportSQLParam = ""; ;
                                    MyReportName = "rptPhuLuc3TongHop.rpt";
                                    MyTableName = "tblPhuLuc3TongHop";
                                }
                            }
                            else
                                if (PhuLucSo_str == "Phụ lục 4")
                                {
                                    if (chb_baocaotonghop.Checked == false)
                                    {
                                        MyReportSQL = "SELECT * FROM dbo.tblPhuLuc4 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                        MyReportParams = lstParams;
                                        MyReportSQLParam = ""; ;
                                        MyReportName = "rptPhuLuc4.rpt";
                                        MyTableName = "tblPhuLuc4";
                                    }
                                    else
                                    {
                                        MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc4 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                        MyReportParams = lstParams;
                                        MyReportSQLParam = ""; ;
                                        MyReportName = "rptPhuLuc4TongHop.rpt";
                                        MyTableName = "tblPhuLuc4TongHop";
                                    }
                                }
                                else
                                    if (PhuLucSo_str == "Phụ lục 5")
                                    {
                                        if (chb_baocaotonghop.Checked == false)
                                        {
                                            MyReportSQL = "SELECT * FROM dbo.tblPhuLuc5 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                            MyReportParams = lstParams;
                                            MyReportSQLParam = ""; ;
                                            MyReportName = "rptPhuLuc5.rpt";
                                            MyTableName = "tblPhuLuc5";
                                        }
                                        else
                                        {
                                            MyReportSQL = "select DAY(GETDATE()) as F2,MONTH(GETDATE()) as F3,YEAR(GETDATE()) as F4,sum(F5) as F5,sum(F6) as F6,sum(F7) as F7,sum(F8) as F8,sum(F9) as F9,sum(F10) as F10,sum(F11) as F11, " +
                                            " sum(F12) as F12,sum(F13) as F13,sum(F14) as F14,sum(F15) as F15,sum(F16) as F16,sum(F17) as F17,sum(F18) as F18,sum(F19) as F19,sum(F20) as F20,sum(F21) as F21, " +
                                            " sum(F22) as F22,sum(F23) as F23,sum(F24) as F24,sum(F25) as F25,sum(F26) as F26,sum(F27) as F27,sum(F28) as F28,sum(F29) as F29 " +
                                            " from tblPhuLuc5 where kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%')";
                                            MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc5 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                            MyReportParams = lstParams;
                                            MyReportSQLParam = ""; ;
                                            MyReportName = "rptPhuLuc5TongHop.rpt";
                                            MyTableName = "tblPhuLuc5TongHop";
                                        }
                                    }
                                    else
                                        if (PhuLucSo_str == "Phụ lục 6")
                                        {
                                            if (chb_baocaotonghop.Checked == false)
                                            {
                                                MyReportSQL = "SELECT * FROM dbo.tblPhuLuc6 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                                MyReportParams = lstParams;
                                                MyReportSQLParam = ""; ;
                                                MyReportName = "rptPhuLuc6.rpt";
                                                MyTableName = "tblPhuLuc6";
                                            }
                                            else
                                            {
                                                MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc6 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                                MyReportParams = lstParams;
                                                MyReportSQLParam = ""; ;
                                                MyReportName = "rptPhuLuc6TongHop.rpt";
                                                MyTableName = "tblPhuLuc6TongHop";
                                            }
                                        }
                                        else
                                            if (PhuLucSo_str == "Phụ lục 7")
                                            {
                                                if (chb_baocaotonghop.Checked == false)
                                                {
                                                    MyReportSQL = "SELECT * FROM dbo.tblPhuLuc7 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                                    MyReportParams = lstParams;
                                                    MyReportSQLParam = ""; ;
                                                    MyReportName = "rptPhuLuc7.rpt";
                                                    MyTableName = "tblPhuLuc7";
                                                }
                                                else
                                                {
                                                    MyReportSQL = "select DAY(GETDATE()) as F2,MONTH(GETDATE()) as F3,YEAR(GETDATE()) as F4,sum(F5) as F5,sum(F6) as F6,sum(F7) as F7,sum(F8) as F8,sum(F9) as F9,sum(F10) as F10,sum(F11) as F11, " +
                                                    " sum(F12) as F12,sum(F13) as F13,sum(F14) as F14,sum(F15) as F15,sum(F16) as F16,sum(F17) as F17,sum(F18) as F18,sum(F19) as F19,sum(F20) as F20,sum(F21) as F21, " +
                                                     " sum(F22) as F22,sum(F23) as F23,sum(F24) as F24,sum(F25) as F25,sum(F26) as F26,sum(F27) as F27,sum(F28) as F28,sum(F29) as F29, " +
                                                     " sum(F30) as F30,sum(F31) as F31,sum(F32) as F32,sum(F33) as F33,sum(F34) as F34,sum(F35) as F35,sum(F36) as F36,sum(F37) as F37 " +
                                                     " from tblPhuLuc7 where kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%')";
                                                    MyReportSQL = "select a.*,b.TenDV " +
                                                " from tblPhuLuc7 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                                    MyReportParams = lstParams;
                                                    MyReportSQLParam = ""; ;
                                                    MyReportName = "rptPhuLuc7TongHop.rpt";
                                                    MyTableName = "tblPhuLuc7TongHop";
                                                }
                                            }
                                            else
                                                if (PhuLucSo_str == "Phụ lục 8")
                                                {
                                                    if (chb_baocaotonghop.Checked == false)
                                                    {
                                                        MyReportSQL = "SELECT * FROM dbo.tblPhuLuc8 WHERE KyBaoCaoID_DM ='" + KyBaoCao.ToString() + "'" + "  AND DonVi_ID = '" + IDDonVi.ToString() + "' ";
                                                        MyReportParams = lstParams;
                                                        MyReportSQLParam = ""; ;
                                                        MyReportName = "rptPhuLuc8.rpt";
                                                        MyTableName = "tblPhuLuc8";
                                                    }
                                                    else
                                                    {
                                                        MyReportSQL = "select a.*,b.TenDV " +
                                                        " from tblPhuLuc8 a, tblDonVi b where a.DonVi_ID=b.id and kybaocaoid_dm=" + KyBaoCao.ToString() + " and DonVi_ID IN (Select ID from tblDonVi where parentID like '%" + IDDonVi.ToString() + "%') order by a.DonVi_ID";
                                                        MyReportParams = lstParams;
                                                        MyReportSQLParam = ""; ;
                                                        MyReportName = "rptPhuLuc8TongHop.rpt";
                                                        MyTableName = "tblPhuLuc8TongHop";
                                                    }
                                                }
                if (hasreport)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportView.aspx');", true);
                }
            }

        }

        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);

        }
        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }
        protected void BindDataToDropDownList(DropDownList ddlist, string MaDM)
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='" + MaDM + "'");
            ddlist.DataSource = dt;
            ddlist.DataTextField = "TenDM";
            ddlist.DataValueField = "ID";
            ddlist.DataBind();
        }
    }
}